package com.lijie.springcloud.entries;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @Authorr 李杰
 * @Date：2022/1/3111:52
 **/
public  class Result<T> {
    private Integer status=-1;
    private String message;
    private T data;
    private List<T> list;

    /** 状态码:成功 */
    public final static Integer STATUS_CODE_SUCCESS = 200;
    /** 状态码:服务器内部错误 */
    public final static Integer STATUS_CODE_500 = 500;
    /** 状态码:未找到要处理的数据 */
    public final static Integer STATUS_CODE_UNFOUND_PROCESS_DATA = 1001;
    /** 状态码:未执行要求的操作 */
    public final static Integer STATUS_CODE_UNPROCESS = 1002;
    public Result() {
    }
    public Result(Integer status,String message){
        this.status = status;
        this.message = message;
    }
    public Result(Integer status) {
        this.status = status;
    }

    public Result(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public void setData(T date) {
        this.data = date;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public static Result getInstanceSuccess() {
        return new Result<>( STATUS_CODE_SUCCESS, "操作成功");
    }
    public static Result getInstanceSuccess(Class data) throws IllegalAccessException, InstantiationException {
        Result result = new Result(STATUS_CODE_SUCCESS, "操作成功");
        Object o = data.newInstance();
        result.setData(o);
        return result;
    }
    public void setErrored(String msg) {
        this.status=(Result.STATUS_CODE_500);
        this.setMessage(msg);
    }
    public void setErrored() {
        this.status=(Result.STATUS_CODE_500);
    }
    public static Result<Object> getInstanceError() {
        return new Result<Object>( STATUS_CODE_500, "未知错误");
    }
    public static Result<Object> getInstanceError(String s) {
        return new Result<Object>( STATUS_CODE_500, s);
    }
    public void setSuccess(){
        this.status=STATUS_CODE_SUCCESS;
    }
    public void setSuccess(String message){
        this.status=STATUS_CODE_SUCCESS;
        this.setMessage(message);
    }
    public void setStatusCodeError(){
        this.status=STATUS_CODE_500;
    }
    public boolean isSuccess() {
        if (STATUS_CODE_SUCCESS.equals(this.getStatus()) ) {
            return true;
        }
        return false;
    }
    public boolean isError(){
        return !isSuccess();
    }
}
